//
//  HPModifyCardViewController.m
//  HPZZTong
//
//  Created by Handpay on 14-3-28.
//  Copyright (c) 2014年 Handpay. All rights reserved.
//

#import "HPModifyCardViewController.h"
#import "HPConditionTextField.h"
#import "ZZTongUtility.h"
#import "CallbackData.h"
#import "ZZTongAutoCompleteViewV2.h"

#define kProvinceViewTag  10001
#define kAreaViewTag      10002
#define kBranchViewTag    10003
#define kCardViewTag      10004

@interface HPModifyCardViewController (){
    BOOL isFirstLoad;
    BOOL isFirstArea;
    IBOutlet UIScrollView *scrollView;
    IBOutlet UILabel *tipsTitleLabel;
    IBOutlet UIView *cardTipsView;
}

@property(nonatomic, retain)NSDictionary* bankDic;
@property(nonatomic, retain)NSDictionary* branchDic;
@property(nonatomic, retain)NSArray* desArray;
@property(nonatomic, retain) NSString* subBankId;
@property(nonatomic, retain)HPDropDownListView* bankDropDownView;
@property(nonatomic, retain)ZZTongAutoCompleteViewV2* branchView;
@property(nonatomic, retain)HPConditionTextField* cardNoTextField;
@property(nonatomic, retain)NSString* areaId;
@property(nonatomic, retain)HPConditionTextField* verifyCodeTextField;
@property(nonatomic, retain)HPDropDownListView* provinceListView;
@property(nonatomic, retain)ZZTongAutoCompleteViewV2* areaView;

@property(nonatomic, retain)NSDictionary* provinceDic;
@property(nonatomic, retain)NSDictionary* areaDic;

@property(nonatomic, retain) NSString* provinceFlag;
@property(nonatomic, retain) NSString* subBankFlag;
@property(nonatomic, retain) NSString* areaFlag;
@property(nonatomic, retain)UIView* cardBgView;

@end

@implementation HPModifyCardViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.desArray = @[@"持卡人", @"收款行", @"开户省份", @"开户地市", @"开户行", @"银行卡号", @"手机号码", @"短信验证码"];
//        self.bankDic = @{@"items":@(1),
//                        @"list":@[@{@"bankId":ZZT_CLIENG.accountInfo.bankId,@"bankName":ZZT_CLIENG.accountInfo.bankName}]};
//        self.branchDic = @{@"items":@(1),
//                    @"list":@[@{@"bankId":ZZT_CLIENG.accountInfo.branchBankId,@"bankName":ZZT_CLIENG.accountInfo.branchBankName}]};
        isFirstLoad = YES;
        isFirstArea = YES;
    }
    return self;
}
#define kCellH  44
- (void)initData{
    NSInteger cardBgH = 5 * kCellH;
    UIView* cardBg = [[UIView alloc]initWithFrame:CGRectMake(0, 20, ScreenWidth, cardBgH)];
    cardBg.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:cardBg];
    self.cardBgView = cardBg;
    [cardBg release];
    
    NSInteger h = 42;
    NSInteger top = 0;
    
    CGRect labelRect = CGRectMake(10, 0, 80, h);
    CGRect valueFrame = CGRectMake(85, 0, 230, h);
    CGRect lineRect = CGRectMake(0, h + 1, ScreenWidth, 1);
    UIColor* lineColor = RGBCOLOR(243, 243, 243);
    
    // 收款人姓名
    UILabel* desLabel = [[UILabel alloc] initWithFrame:labelRect];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    desLabel.text = [self.desArray objectAtIndex:0];
    [self.cardBgView addSubview:desLabel];
    [desLabel release];
    
    UILabel* valueLabel = [[UILabel alloc] initWithFrame:valueFrame];
    valueLabel.textColor = [UIColor blackColor];
    valueLabel.numberOfLines = 0;
    valueLabel.font = kNormalFont;
    valueLabel.text = ZZT_CLIENG.accountInfo.name;
    [Utility adjustFontSizeWithLable:valueLabel size:16];
    [self.cardBgView addSubview:valueLabel];
    [valueLabel release];
    
    //line
    UIView* blineView = [[UIView alloc]initWithFrame:lineRect];
    blineView.backgroundColor = lineColor;
    [self.cardBgView addSubview:blineView];
    [blineView release];
    
    top += kCellH;
    //收款行
    desLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, top, 80, h)];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    desLabel.text = [self.desArray objectAtIndex:1];
    [self.cardBgView addSubview:desLabel];
    [desLabel release];
    
    HPDropDownListView* dropListView = [[HPDropDownListView alloc] initWithFrame:CGRectMake(75, top, 240, h)];
    [dropListView setShowColor:kTitleFontColor];
    [dropListView setPlaceholder:@"请选择银行"];
    self.bankDropDownView = dropListView;
    self.bankDropDownView.delegate = self;
    [self.bankDropDownView setHPtext:ZZT_CLIENG.accountInfo.bankName];
    [self.cardBgView addSubview:dropListView];
    [dropListView release];
    
    blineView = [[UIView alloc]initWithFrame:CGRectMake(0, top + h + 1, ScreenWidth, 1)];
    blineView.backgroundColor = lineColor;
    [self.cardBgView addSubview:blineView];
    [blineView release];
    
    top += kCellH;
    //开户省份
    UIView* tempView = [[UIView alloc]initWithFrame:CGRectMake(0, top, ScreenWidth, kCellH)];
    tempView.backgroundColor = [UIColor clearColor];
    tempView.hidden = YES;
    tempView.tag = kProvinceViewTag;
    
    desLabel = [[UILabel alloc] initWithFrame:labelRect];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    desLabel.text = [self.desArray objectAtIndex:2];
    [tempView addSubview:desLabel];
    [desLabel release];
    
    dropListView = [[HPDropDownListView alloc] initWithFrame:CGRectMake(75, 0, 240, h)];
    [dropListView setShowColor:kTitleFontColor];
    [dropListView setPlaceholder:@"请选择开户省份"];
    self.provinceListView = dropListView;
    self.provinceListView.delegate = self;
    if (ZZT_CLIENG.accountInfo.provinceName) {
        [self.provinceListView setHPtext:ZZT_CLIENG.accountInfo.provinceName];
    }
    
    [tempView addSubview:dropListView];
    [dropListView release];

    blineView = [[UIView alloc]initWithFrame:lineRect];
    blineView.backgroundColor = lineColor;
    [tempView addSubview:blineView];
    [blineView release];
    
    [self.cardBgView addSubview:tempView];
    [tempView release];

    //开户地区
    tempView = [[UIView alloc]initWithFrame:CGRectMake(0, top, ScreenWidth, kCellH)];
    tempView.backgroundColor = [UIColor clearColor];
    tempView.hidden = YES;
    tempView.tag = kAreaViewTag;
    
    desLabel = [[UILabel alloc] initWithFrame:labelRect];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    desLabel.text = [self.desArray objectAtIndex:3];
    [tempView addSubview:desLabel];
    [desLabel release];

    ZZTongAutoCompleteViewV2* autoView = [[ZZTongAutoCompleteViewV2 alloc] initWithFrame:valueFrame];
    [autoView initViewStyle];
    [autoView setHint:@"请输入开户地市"];
    if (ZZT_CLIENG.accountInfo.areaName) {
        [autoView setText:ZZT_CLIENG.accountInfo.areaName];
    }
    [autoView setTableWidth:ScreenWidth];
    autoView.delegate = self;
    self.areaView = autoView;
    [autoView release];
    [tempView addSubview:self.areaView];
    
    blineView = [[UIView alloc]initWithFrame:lineRect];
    blineView.backgroundColor = lineColor;
    [tempView addSubview:blineView];
    [blineView release];
    
    [self.cardBgView addSubview:tempView];
    [tempView release];
    
    //开户行
    tempView = [[UIView alloc]initWithFrame:CGRectMake(0, top, ScreenWidth, kCellH)];
    tempView.backgroundColor = [UIColor clearColor];
    tempView.hidden = YES;
    tempView.tag = kBranchViewTag;
    
    desLabel = [[UILabel alloc] initWithFrame:labelRect];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    desLabel.text = [self.desArray objectAtIndex:4];
    [tempView addSubview:desLabel];
    [desLabel release];
    
    autoView = [[ZZTongAutoCompleteViewV2 alloc] initWithFrame:valueFrame];
    [autoView initViewStyle];
    [autoView setHint:@"请输入开户行"];
    if (ZZT_CLIENG.accountInfo.branchBankName) {
        [autoView setText:ZZT_CLIENG.accountInfo.branchBankName];
    }
    [autoView setTableWidth:ScreenWidth];
    autoView.delegate = self;
    self.branchView = autoView;
    [autoView release];
    [tempView addSubview:self.branchView];
    
    blineView = [[UIView alloc]initWithFrame:lineRect];
    blineView.backgroundColor = lineColor;
    [tempView addSubview:blineView];
    [blineView release];
    
    [self.cardBgView addSubview:tempView];
    [tempView release];
    
    //////
    tempView = [[UIView alloc]initWithFrame:CGRectMake(0, top, ScreenWidth, kCellH * 3)];
    tempView.backgroundColor = [UIColor clearColor];
    tempView.tag = kCardViewTag;
    //卡号
    desLabel = [[UILabel alloc] initWithFrame:labelRect];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    desLabel.text = [self.desArray objectAtIndex:5];
    [tempView addSubview:desLabel];
    [desLabel release];
    
    HPConditionTextField* textField = [[HPConditionTextField alloc] initWithFrame:valueFrame];
    textField.textColor = kTitleFontColor;
    textField.font = kNormalFont;
    textField.placeholder = @"请输入卡号";
    [textField setConditionType:HPConditionType_CardNo];
    textField.text = [Utility encryptCardID:ZZT_CLIENG.accountInfo.cardNo];
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.cardNoTextField = textField;
    [tempView addSubview:textField];
    [textField release];
    
    blineView = [[UIView alloc]initWithFrame:lineRect];
    blineView.backgroundColor = lineColor;
    [tempView addSubview:blineView];
    [blineView release];
    
    //手机号
    desLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, kCellH, 80, h)];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    desLabel.text = [self.desArray objectAtIndex:6];
    [tempView addSubview:desLabel];
    [desLabel release];
    
    valueLabel = [[UILabel alloc] initWithFrame:CGRectMake(85, kCellH, 230, h)];
    valueLabel.textColor = kNormalFontColor;
    valueLabel.font = kNormalFont;
    valueLabel.text = [ZZTongUtility maskMobile:ZZT_CLIENG.accountInfo.mobile];
    [tempView addSubview:valueLabel];
    [valueLabel release];
    
    blineView = [[UIView alloc]initWithFrame:CGRectMake(0, kCellH + h + 1, ScreenWidth, 1)];
    blineView.backgroundColor = lineColor;
    [tempView addSubview:blineView];
    [blineView release];
    
    desLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 2 * kCellH, 80, h)];
    desLabel.textColor = kNormalFontColor;
    desLabel.font = kNormalFont;
    desLabel.text = [self.desArray objectAtIndex:7];
    [tempView addSubview:desLabel];
    [desLabel release];
    
    textField = [[HPConditionTextField alloc] initWithFrame:CGRectMake(105, 2 * kCellH, 105, kCellH)];
    textField.textColor = kTitleFontColor;
    textField.font = kNormalFont;
    textField.conditionType = HPConditionType_Numeric;
    textField.maxLength = 6;
    self.verifyCodeTextField = textField;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [tempView addSubview:textField];
    [textField release];
    
    UIButton* getVerifyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    getVerifyButton.frame = CGRectMake(215, 2 * kCellH + 2, 100, 40);
    [getVerifyButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [getVerifyButton setTitleColor:kNormalFontColor forState:UIControlStateNormal];
    [getVerifyButton setBackgroundImage:[[UIImage imageNamed:@"smallbutton.png"] stretchableImageWithLeftCapWidth:6 topCapHeight:0] forState:UIControlStateNormal];
    [getVerifyButton addTarget:self action:@selector(getVerifyButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    getVerifyButton.titleLabel.font = kNormalFont;
    [tempView addSubview:getVerifyButton];
    
    blineView = [[UIView alloc]initWithFrame:CGRectMake(0, 2 * kCellH + h + 1, ScreenWidth, 1)];
    blineView.backgroundColor = lineColor;
    [tempView addSubview:blineView];
    [blineView release];
    
    [self.cardBgView addSubview:tempView];
    [tempView release];
}
- (void)refreshCardView{
    UIView* provView = [self.cardBgView viewWithTag:kProvinceViewTag];
    UIView* areaView = [self.cardBgView viewWithTag:kAreaViewTag];
    UIView* branchBgView = [self.cardBgView viewWithTag:kBranchViewTag];
    UIView* cardView = [self.cardBgView viewWithTag:kCardViewTag];
    
    provView.hidden = YES;
    areaView.hidden = YES;
    branchBgView.hidden = YES;
    
    NSInteger top = 2 * kCellH;
    if (![NSString isNilOrEmpty:self.provinceFlag] && [self.provinceFlag isEqualToString:@"1"]) {
        provView.hidden = NO;
        top += kCellH;
    }
    
    if (![NSString isNilOrEmpty:self.areaFlag] && [self.areaFlag isEqualToString:@"1"]) {
        areaView.hidden = NO;
        CGRect frame = areaView.frame;
        frame.origin.y = top;
        areaView.frame = frame;
        top += kCellH;
    }
    if (![NSString isNilOrEmpty:self.subBankFlag] && [self.subBankFlag isEqualToString:@"1"]) {
        branchBgView.hidden = NO;
        CGRect frame = branchBgView.frame;
        frame.origin.y = top;
        branchBgView.frame = frame;
        top += kCellH;
    }
    CGRect frame = cardView.frame;
    frame.origin.y = top;
    cardView.frame = frame;
    top += frame.size.height;
    
    frame = self.cardBgView.frame;
    frame.size.height = top;
    self.cardBgView.frame = frame;
    
    CGRect tipsFrame = cardTipsView.frame;
    tipsFrame.origin.y = frame.origin.y + frame.size.height + 10;
    cardTipsView.frame = tipsFrame;
    
    CGRect buttonFrame = self.saveButton.frame;
    buttonFrame.origin.y = tipsFrame.origin.y + tipsFrame.size.height + 5;
    self.saveButton.frame = buttonFrame;
    
    scrollView.contentSize = CGSizeMake(scrollView.contentSize.width, buttonFrame.origin.y + buttonFrame.size.height + 10);
}
- (void)refreshList:(NSString*)keyId{
    
    NSDictionary* dataDic = nil;
    NSString* nameKey = nil;
    NSString* idKey = nil;
    NSString* sourId = nil;
    HPDropDownListView* listView = nil;
    
    if ([keyId isEqualToString:@"province"]) {
        listView = self.provinceListView;
        dataDic = self.provinceDic;
        nameKey = @"provinceName";
        idKey = @"provinceId";
        if(isFirstLoad){
            sourId = ZZT_CLIENG.accountInfo.provinceId;
        }
        isFirstLoad = NO;
        
    }else if ([keyId isEqualToString:@"bank"]) {
        listView = self.bankDropDownView;
        dataDic = self.bankDic;
        nameKey = @"bankName";
        idKey = @"bankCode";
        sourId = ZZT_CLIENG.accountInfo.bankId;
    }
    
    if (listView) {
        NSInteger count = [[dataDic objectForKey:@"items"] integerValue];
        NSArray* bankArray = [dataDic objectForKey:@"list"];
        
        NSMutableArray* itemsArray = nil;
        NSInteger index = -1;
        if (bankArray && bankArray.count == count) {
            itemsArray = [NSMutableArray array];
            
            for (NSInteger i = 0; i < bankArray.count; i++) {
                NSDictionary* dic = [bankArray objectAtIndex:i];
                [itemsArray addObject:[dic objectForKey:nameKey]];
                if (sourId) {
                    NSString* tempId = [dic objectForKey:idKey];
                    if ([tempId isEqualToString:sourId]) {
                        index = i;
                    }
                }
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^(){
            [listView setHPitems:itemsArray];
            if (index != -1) {
                [listView setHPselectedindex:@(index + 1)];
            }
            [self refreshCardView];
        });
    }
}

- (void)parseGetBanks:(NSDictionary *)respData{
    self.bankDic = respData;
    [self refreshList:@"bank"];
}
- (void)parseGetProvince:(NSDictionary *)respData{
    self.provinceDic = respData;
    
    [self refreshList:@"province"];
}
- (void)viewDidLoad
{
    self.title = @"修改收款卡";
    [super viewDidLoad];
    
    [self.saveButton setBackgroundImage:[[UIImage imageNamed:@"bluebutton.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateNormal];
    
    [self.saveButton setBackgroundImage:[[UIImage imageNamed:@"bluebuttonhover.png"] stretchableImageWithLeftCapWidth:9 topCapHeight:0] forState:UIControlStateHighlighted];
    
    scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, self.saveButton.frame.origin.y + self.saveButton.frame.size.height + 10);
    
    tipsTitleLabel.textColor = kTitleFontColor;
    
    AccountInfoData* accountInfo = ZZT_CLIENG.accountInfo;
    if (![NSString isNilOrEmpty:accountInfo.branchBankName]) {
        self.subBankFlag = @"1";
    }
    if (![NSString isNilOrEmpty:accountInfo.provinceName]) {
        self.provinceFlag = @"1";
    }
    if (![NSString isNilOrEmpty:accountInfo.areaName]) {
        self.areaFlag = @"1";
    }
    
    [self initData];
    [self parseGetBanks:self.screenData];
}


- (void)getVerifyButtonClick:(UIButton*)button{
    NSDictionary* params = @{KEY_ACTION:@"changeBindCard", KEY_ACCOUNT:DES_ENC(ZZT_CLIENG.accountInfo.mobile), KEY_CHANNEL: [ZZT_CLIENG getChannel]};
    [ZZT_CLIENG doSendVerifyCode:params withButton:button];
}

- (void)searchWordChanged:(ZZTongAutoCompleteViewV2*)view word:(NSString*)word{
    isFirstArea = NO;
    isFirstLoad = NO;
    
    if ([NSString isNilOrEmpty:word]){
        [view changeSearchArray:nil];
        return;
    }
    NSNumber* indexNum = (NSNumber*)[self.bankDropDownView readHPselectedindex];
    if (!indexNum) {
        return;
    }
    
    NSInteger indexBank = [indexNum integerValue];
    if (indexBank < 0) {
        return;
    }
    NSString* bankId = [[[self.bankDic objectForKey:@"list"] objectAtIndex:indexBank] objectForKey:@"bankCode"];
    
    if (!bankId) {
        [view changeSearchArray:nil];
        return;
    }
    ZZTongClientEngine* zzt = ZZT_CLIENG;
    NSDictionary* params = @{@"bankCode": bankId,
                             @"searchText": word,
                             KEY_CHANNEL: [zzt getChannel],
                             @"pageSize":@"30"
                             };
    if([view isEqual:self.areaView]){
        NSInteger indexProvince = -1;
        NSNumber* indexProNumber = (NSNumber*)[self.provinceListView readHPselectedindex];
        if (indexProNumber) {
            indexProvince = [indexProNumber integerValue];
        }
        NSString* provinceId = nil;
        if (indexProvince >= 0) {
            provinceId = [[[self.provinceDic objectForKey:@"list"] objectAtIndex:indexProvince] objectForKey:@"provinceId"];
        }
        
        
        NSMutableDictionary* mutParams = [NSMutableDictionary dictionaryWithDictionary:params];
        if (provinceId) {
            [mutParams setObject:provinceId forKey:@"provinceId"];
        }
        LOG(@"params:%@", params);
        [zzt.netEngine sendRequest:NET_ACTION_BANK_AREA withParams:mutParams withDelegate:self];
    }else if([view isEqual:self.branchView]){
        [zzt.netEngine sendRequest:NET_ACTION_GET_BRANCHS withParams:params withDelegate:self];
    }
}

- (void)dealloc {
    self.bankDic = nil;
    self.desArray = nil;
    self.bankDropDownView = nil;
    self.branchView = nil;
    self.cardNoTextField = nil;
    self.branchDic = nil;
    self.branchView = nil;
    self.verifyCodeTextField = nil;
    self.provinceFlag = nil;
    self.subBankFlag = nil;
    self.areaFlag = nil;
    self.provinceListView = nil;
    self.areaView = nil;
    self.cardBgView = nil;
    self.areaId = nil;
    self.provinceDic = nil;
    self.areaDic = nil;
    
    self.areaId = nil;
    self.subBankId = nil;
    
//    [_cardTableView release];
    [_saveButton release];
    [scrollView release];
    [tipsTitleLabel release];
    [cardTipsView release];
    [super dealloc];
}

- (void)viewDidUnload {
//    [self setCardTableView:nil];
    [self setSaveButton:nil];
    [scrollView release];
    scrollView = nil;
    [super viewDidUnload];
}

- (void)responseCallBackSuccess:(NSString *)action withData:(NSDictionary *)respData{
    [HPUIENG removeWaiting];
    if ([action isEqualToString:NET_ACTION_MODIFY_CARDNO]){
        
        ZZT_CLIENG.accountInfo.cardNo = [self.cardNoTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        NSNumber* indexNum = (NSNumber*)[self.bankDropDownView readHPselectedindex];
        NSInteger index = [indexNum integerValue];
        
        NSDictionary* bankInfo = [[self.bankDic objectForKey:@"list"] objectAtIndex:index];
        NSString* bankName = [bankInfo objectForKey:@"bankName"];
        NSString* bankCode = [bankInfo objectForKey:@"bankCode"];
        
        ZZT_CLIENG.accountInfo.bankId = bankCode;
        ZZT_CLIENG.accountInfo.bankName = bankName;
        
        if (![NSString isNilOrEmpty:self.provinceFlag] && [self.provinceFlag isEqualToString:@"1"]) {
            NSNumber* provinceIndexNum = (NSNumber*)[self.provinceListView readHPselectedindex];
            NSInteger provinceIndex = [provinceIndexNum integerValue];
            NSDictionary* provinceInfo = [[self.provinceDic objectForKey:@"list"] objectAtIndex:provinceIndex];
            ZZT_CLIENG.accountInfo.provinceName = [provinceInfo objectForKey:@"provinceName"];
            ZZT_CLIENG.accountInfo.provinceId = [provinceInfo objectForKey:@"provinceId"];
        }else{
            ZZT_CLIENG.accountInfo.provinceName = nil;
            ZZT_CLIENG.accountInfo.provinceId = nil;
        }
        
        if (![NSString isNilOrEmpty:self.areaFlag] && [self.areaFlag isEqualToString:@"1"]) {
            ZZT_CLIENG.accountInfo.areaId = self.areaId;
            ZZT_CLIENG.accountInfo.areaName = [self.areaView readText];
        }else{
            ZZT_CLIENG.accountInfo.areaId = nil;
            ZZT_CLIENG.accountInfo.areaName = nil;
        }
        
        if (![NSString isNilOrEmpty:self.subBankFlag] && [self.subBankFlag isEqualToString:@"1"]) {
            ZZT_CLIENG.accountInfo.branchBankId = self.subBankId;
            ZZT_CLIENG.accountInfo.branchBankName = [self.branchView readText];
        }else{
            ZZT_CLIENG.accountInfo.branchBankId = nil;
            ZZT_CLIENG.accountInfo.branchBankName = nil;
        }
        
        CallbackData* callback = [self.screenData objectForKey:KEY_CALLBACK];
        [callback.target performSelectorOnMainThread:callback.callback withObject:nil waitUntilDone:YES];
        
        AlertCommandData* commandCancel = [[AlertCommandData alloc] initCommand:@"确定" function:@selector(goBack)];
        AlertData* alertData = [[AlertData alloc] initAlertData:self title:@"提示" message:@"收款卡修改成功" commands:commandCancel, nil];
        [HPUIENG showAlertWithData:alertData];
        
        [commandCancel release];
        [alertData release];
    }else if ([action isEqualToString:NET_ACTION_GET_BRANCHS]){
        [self parseGetBranchs:respData];
    }else if ([action isEqualToString:NET_ACTION_BANK_PROVINCE]){
        [self parseGetProvince:respData];
    }else if ([action isEqualToString:NET_ACTION_BANK_AREA]){
        [self parseGetArea:respData];
    }
}
- (void)parseGetBranchs:(NSDictionary *)respData{
    self.branchDic = respData;
    
    NSInteger count = [[respData objectForKey:@"items"] integerValue];
    NSArray* branchArray = [respData objectForKey:@"list"];
    
    NSMutableArray* itemsArray = nil;
    
    if (branchArray && branchArray.count == count) {
        itemsArray = [NSMutableArray array];
        
        for (NSDictionary* dic in branchArray) {
            [itemsArray addObject:[dic objectForKey:@"subBankName"]];
        }
    }
    
    dispatch_async(dispatch_get_main_queue(), ^(){
        [self.branchView changeSearchArray:itemsArray];
    });
}
- (void)parseGetArea:(NSDictionary *)respData{
    self.areaDic = respData;
    
    NSInteger count = [[respData objectForKey:@"items"] integerValue];
    NSArray* branchArray = [respData objectForKey:@"list"];
    
    NSMutableArray* itemsArray = nil;
    
    if (branchArray && branchArray.count == count) {
        itemsArray = [NSMutableArray array];
        
        for (NSDictionary* dic in branchArray) {
            [itemsArray addObject:[dic objectForKey:@"areaName"]];
        }
    }
    
    dispatch_async(dispatch_get_main_queue(), ^(){
        [self.areaView changeSearchArray:itemsArray];
    });
}
- (void)responseCallBackFailed:(NSString *)action withErrCode:(NSString *)errCode withErrMsg:(NSString *)msg{
    if ([action isEqualToString:NET_ACTION_GET_BRANCHS]) {
        self.branchDic = nil;
        dispatch_async(dispatch_get_main_queue(), ^(){
            [self.branchView changeSearchArray:nil];
        });
    }else if ([action isEqualToString:NET_ACTION_BANK_AREA]) {
        self.areaDic = nil;
        dispatch_async(dispatch_get_main_queue(), ^(){
            [self.areaView changeSearchArray:nil];
        });
    }else if ([action isEqualToString:NET_ACTION_BANK_PROVINCE]) {
        self.provinceDic = nil;
        dispatch_async(dispatch_get_main_queue(), ^(){
            [self refreshList:@"province"];
        });
    }
}

- (IBAction)saveButtonClick:(id)sender {
    [self hideKeyboard];
    //收款行
    if (!self.bankDic) {
        [HPUIENG showAlert:@"请选择收款行"];
        return;
    }
    
    NSNumber* indexNum = (NSNumber*)[self.bankDropDownView readHPselectedindex];
    NSInteger index = [indexNum integerValue];
    if (index == -1) {
        [HPUIENG showAlert:@"请选择银行"];
        return;
    }
    
    NSDictionary* bankInfo = [[self.bankDic objectForKey:@"list"] objectAtIndex:index];
    if (!bankInfo) {
        [HPUIENG showAlert:@"请选择银行"];
        return;
    }
    NSString* bankCode = [bankInfo objectForKey:@"bankCode"];
    //开户省份
    NSString* provinceId = nil;
    if (![NSString isNilOrEmpty:self.provinceFlag] && [self.provinceFlag isEqualToString:@"1"]) {
        if (!self.provinceDic) {
            [HPUIENG showAlert:@"请选择开户省份"];
            return;
        }
        
        indexNum = (NSNumber*)[self.provinceListView readHPselectedindex];
        index = [indexNum integerValue];
        if (index == -1) {
            [HPUIENG showAlert:@"请选择开户省份"];
            return;
        }
        
        NSDictionary* provinceInfo = [[self.provinceDic objectForKey:@"list"] objectAtIndex:index];
        if (!provinceInfo) {
            [HPUIENG showAlert:@"请选择开户省份"];
            return;
        }
        provinceId = [provinceInfo objectForKey:@"provinceId"];
    }
    
    //开户地市
    NSString* subAreaId = nil;
    if (![NSString isNilOrEmpty:self.areaFlag] && [self.areaFlag isEqualToString:@"1"]) {
        NSString* area = [self.areaView readText];
        if ([NSString isNilOrEmpty:area]) {
            [HPUIENG showAlert:@"请输入开户地市"];
            return;
        }
        if (!self.areaDic) {
            subAreaId = ZZT_CLIENG.accountInfo.areaId;
        }else{
            BOOL isFound = NO;
        
            NSArray* areaArray = [self.areaDic objectForKey:@"list"];
            for (NSDictionary* areaInfo in areaArray) {
                NSString* name = [areaInfo objectForKey:@"areaName"];
                if ([name isEqualToString:area]) {
                    isFound = YES;
                    subAreaId = [areaInfo objectForKey:@"areaId"];
                    break;
                }
            }
        
            if (!isFound) {
                [HPUIENG showAlert:@"请输入正确的开户地市"];
                return;
            }
        }
    }
    NSString* subBankId = nil;
    //开户行
    if (![NSString isNilOrEmpty:self.subBankFlag] && [self.subBankFlag isEqualToString:@"1"]){
        NSString* branch = [self.branchView readText];
        if ([NSString isNilOrEmpty:branch]) {
            [HPUIENG showAlert:@"请输入开户行"];
            return;
        }
        if (!self.branchDic) {
            subBankId = ZZT_CLIENG.accountInfo.branchBankId;
        }else{
            BOOL isFound = NO;
        
            NSArray* branchArray = [self.branchDic objectForKey:@"list"];
            for (NSDictionary* branchInfo in branchArray) {
                NSString* name = [branchInfo objectForKey:@"subBankName"];
                if ([name isEqualToString:branch]) {
                    isFound = YES;
                    subBankId = [branchInfo objectForKey:@"subBankCode"];
                    break;
                }
            }
        
            if (!isFound) {
                [HPUIENG showAlert:@"请输入正确的开户行"];
                return;
            }
        }
    }
    
    NSString* tempCardNo = [self.cardNoTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (![ZZTongUtility checkIsInput:self.cardNoTextField tips:@"银行卡号"]) {
        return;
    }
    NSRange range = [tempCardNo rangeOfString:@"*"];
    if (range.length != 0) {
        [HPUIENG showAlert:@"请输入合法的银行卡卡号"];
        return;
    }
    if (![Utility isLegalCard:tempCardNo length:0]) {
        [HPUIENG showAlert:@"请输入正确的银行卡号"];
        return;
    }

    
    if (![ZZTongUtility checkLength:self.verifyCodeTextField tips:@"请输入6位短信验证码" minLen:6 maxLen:6]) {
        return;
    }
    
    NSString* cardNo = [self.cardNoTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
   
    
    ZZTongClientEngine* zzt = ZZT_CLIENG;
    
    NSDictionary* params = @{KEY_PHONE: [HPCLIENTENG.securmanager Des:zzt.accountInfo.mobile withKey:nil withflag:1],
                             @"name": [HPCLIENTENG.securmanager Des:zzt.accountInfo.name withKey:nil withflag:1],
                             @"cardNum": [HPCLIENTENG.securmanager Des:cardNo withKey:nil withflag:1],
                             KEY_CHANNEL: [zzt getChannel],
                             @"account":DES_ENC(zzt.accountInfo.account),
                             KEY_VERIFYCODE: self.verifyCodeTextField.text,
                             KEY_ACTION: @"changeBindCard",
                             KEY_NEWBANK_FLAG:@"1",
                             @"bankCode":bankCode};
    
    NSMutableDictionary* mutParams = [[[NSMutableDictionary alloc]initWithDictionary:params]autorelease];
    
    self.areaId = subAreaId;
    self.subBankId = subBankId;
    
    if (provinceId) {
        [mutParams setObject:provinceId forKey:@"provinceId"];
    }
    
    if (subBankId) {
        [mutParams setObject:subBankId forKey:@"subBankCode"];
    }
    if (subAreaId) {
        [mutParams setObject:subAreaId forKey:@"areaId"];
    }
    
    LOG(@"params:%@", mutParams);
#if ZZT_FOR_TEST
    [self responseCallBackSuccess:NET_ACTION_MODIFY_CARDNO withData:nil];
#else
    [zzt.netEngine sendRequest:NET_ACTION_MODIFY_CARDNO withParams:mutParams withDelegate:self];
    [zzt.uiEngine showNetWaiting];
#endif
}
- (NSInteger)listSelectedId:(HPDropDownListView*)listView{
    NSInteger index = 0;
    if (listView) {
        NSNumber* selectId = (NSNumber*)[listView readHPselectedindex];
        if (selectId) {
            index = selectId.intValue;
            if (index < 0) {
                index = 0;
            }
        }
    }
    return index;
}
- (void)sendProvinceReq{
    ZZTongClientEngine* zzt = ZZT_CLIENG;
    NSInteger index = [self listSelectedId:self.bankDropDownView];
    NSString* bankCode = [[[self.bankDic objectForKey:@"list"] objectAtIndex:index] objectForKey:@"bankCode"];
    
    NSDictionary* params = @{KEY_CHANNEL: [zzt getChannel],
                             @"bankCode": bankCode,
                             @"pageSize": @"0"};
    LOG(@"params:%@", params);
    [zzt.netEngine sendRequest:NET_ACTION_BANK_PROVINCE withParams:params withDelegate:self];
    
    [zzt.uiEngine showNetWaiting];
}
- (void) dropDownListView:(HPDropDownListView *)dropDownListView selected:(NSArray *)indexs texts:(NSArray *)texts{

    if ([dropDownListView isEqual:self.bankDropDownView]) {
        if (!isFirstLoad) {
            if (self.branchView) {
                [self.branchView setText:@""];
            }
            if (self.areaView) {
                [self.areaView setText:@""];
            }
            if (self.provinceListView) {
                [self.provinceListView setHPtext:@""];
            }
        }
//        isFirstLoad = NO;
        
        NSInteger index = [self listSelectedId:self.bankDropDownView];
        self.provinceFlag = [[[self.bankDic objectForKey:@"list"] objectAtIndex:index] objectForKey:@"provinceFlag"];
        self.subBankFlag = [[[self.bankDic objectForKey:@"list"] objectAtIndex:index] objectForKey:@"subBankFlag"];
        self.areaFlag = [[[self.bankDic objectForKey:@"list"] objectAtIndex:index] objectForKey:@"areaFlag"];
        
        if (![NSString isNilOrEmpty:self.provinceFlag] && [self.provinceFlag isEqualToString:@"1"]) {
            [self sendProvinceReq];
            return;
        }
        isFirstLoad = NO;
        
    }else if([dropDownListView isEqual:self.provinceListView]){
        if (!isFirstArea) {
            if (self.areaView) {
                [self.areaView setText:@""];
            }
        }
        isFirstArea = NO;
        
        if (![NSString isNilOrEmpty:self.areaFlag] && [self.areaFlag isEqualToString:@"1"]) {
            
        }
    }
    [self refreshCardView];
    
}
@end
